<template>
  <div class="sort">
    <el-row :gutter="20">
      <el-col>
        <!-- 一级分类 -->
        <div class="sort-one">
          <!-- 添加按钮 -->
          <el-row class="sort-one-head">
            <el-col :span="3">
              <div class="sort-one-name">产品分类</div>
            </el-col>
            <el-col
              :xs="4"
              :sm="4"
              :md="3"
              :lg="2"
              :xl="2"
              class="sort-one-add"
            >
              <el-button
                type="primary"
                class="sort-one-add-submit"
                size="small"
                @click="addOneSort(null)"
              >
                添加</el-button
              >
            </el-col>
          </el-row>
          <!-- 一级分类列表 -->
          <el-row class="sort-one-list">
            <el-col :span="24">
              <el-table :data="tableData" border style="width: 100%">
                <el-table-column type="index" label="序" width="50">
                </el-table-column>
                <el-table-column label="类型图片" width="120">
                  <template slot-scope="scope">
                    <img width="100%" :src="scope.row.imageUrl" alt="" />
                  </template>
                </el-table-column>
                <el-table-column prop="typeName" label="一级分类名称">
                </el-table-column>
                <el-table-column prop="address" label="操作">
                  <template slot-scope="scope">
                    <el-button
                      size="mini"
                      type="info"
                      @click="addOneSort(scope.row)"
                      >修改</el-button
                    >
                    <el-button
                      size="mini"
                      type="danger"
                      @click="selected = scope.row"
                      >删除</el-button
                    >
                  </template>
                </el-table-column>
              </el-table>
            </el-col>
          </el-row>
          <!-- 分页 -->
          <el-row class="sort-one-page">
            <el-col :span="24" class="sort-one-page-line">
              <el-pagination
                :hide-on-single-page="true"
                background
                layout="prev, pager, next"
                :total="total"
              >
              </el-pagination>
            </el-col>
          </el-row>
        </div>
      </el-col>
    </el-row>
    <!-- 一级分类弹出层 -->
    <el-dialog title="添加产品分类" :visible.sync="oneSortShwo">
      <el-form :model="oneSortForm" :rules="oneSortRules" ref="oneSortForm">
        <el-form-item
          label="分类名称"
          :label-width="formLabelWidth"
          prop="name"
        >
          <el-input
            v-model="oneSortForm.typeName"
            autocomplete="off"
            placeholder="请输入2~4个字符"
          ></el-input>
        </el-form-item>

        <el-form-item
          label="商品图片"
          :label-width="formLabelWidth"
          prop="goodsImage"
        >
          <el-upload
            :action="uploadbaseurl + '/upload'"
            list-type="picture-card"
            multiple
            :limit="1"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
            :on-success="uploadSuccess"
            :on-exceed="message"
            :file-list="filelist"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="oneSortForm.imageUrl" alt="" />
          </el-dialog>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="oneSortSUbmit('oneSortForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <el-dialog title="删除分类" :visible.sync="selected">
      <span>确定删除？</span>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="selected = null">取 消</el-button>
        <el-button size="mini" type="danger" @click="deleteSort()"
          >删 除</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selected: null,
      tableData: [],
      filelist: [],
      dialogVisible: false,
      total: 0,
      // 一级分类
      oneSortShwo: false,
      uploadbaseurl: this.baseurl,
      oneSortForm: {
        typeName: "",
        imageUrl: "",
      },
      oneSortRules: {
        typeName: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          { min: 2, max: 4, message: "长度在 2 到 4 个字符", trigger: "blur" },
        ],
      },
      formLabelWidth: "120px",
    };
  },
  watch: {
    oneSortShwo() {
      if (!this.oneSortShwo) (this.filelist = []), this.initData();
    },
  },
  created() {
    this.initData();
  },
  methods: {
    async initData() {
      let res = await this.http.post("/type/getGoodsTypeList");
      this.tableData = !res.error ? res.result : [];
      this.total = !res.error ? res.result.length : 0;
    },
    addOneSort(item) {
      if (item) {
        this.filelist.push({ url: item.imageUrl });
        this.oneSortForm = item;
      } else {
          console.log("dian");
        this.filelist = [];
        this.oneSortForm = {
          typeName: "",
          imageUrl: "",
        };
      }
      console.log(this.oneSortForm);
      console.log(this.filelist);
      this.oneSortShwo = true;
    },

    oneSortSUbmit(formName) {
      if (this.oneSortForm.imageUrl == "") {
        this.eui.Message({
          message: "请上传类别图片",
          type: "error",
        });
        return false;
      }
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let res = await this.http.post(
            "/type/saveGoodsType",
            this.oneSortForm
          );
          if (!res.error) {
            this.eui.Message({
              message: "操作成功",
              type: "success",
            });
            this.oneSortShwo = false;
            this.initData();
          } else {
            this.eui.Message({
              message: "操作失败",
              type: "error",
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    async deleteSort() {
      let res = await this.http.get(
        "/type/deleteGoodsType?id=" + this.selected.id
      );
      if (!res.error) {
        this.eui.Message({
          message: "操作成功",
          type: "success",
        });
        this.selected = null;
        this.initData();
      } else {
        this.eui.Message({
          message: "操作失败",
          type: "error",
        });
      }
    },
    handleRemove(file, fileList) {
      this.oneSortForm.imageUrl = "";
    },
    handlePictureCardPreview(file) {
      console.log(file.url);
      console.log("dd");
      this.oneSortForm.imageUrl = file.url;
      this.dialogVisible = true;
    },
    uploadSuccess(response, file, fileList) {
      this.oneSortForm.imageUrl = response.result[0];
    },
    dealFileList(fileList) {
      var list = [];
      fileList.forEach((element) => {
        element.response.result.forEach((e) => {
          list.push(e);
        });
      });
      return list;
    },
    message(msg, type) {
      this.eui.Message({
        message: "超出上传个数，最多只能上传1张商品图片",
        type: "error",
      });
    },
  },
};
</script>

<style scoped>
.sort {
  width: 100%;
  min-height: 100%;
  padding: 15px;
  box-sizing: border-box;
}
/* 一级分类 */
.sort-one {
  width: 100%;
  height: auto;
  padding: 10px;
  box-sizing: border-box;
  border-radius: 5px;
  border: 1px solid #eeeeee;
}
.sort-one-name {
  width: 100%;
  height: 50px;
  line-height: 50px;
  padding-left: 20px;
  font-size: 16px;
  font-weight: bold;
  letter-spacing: 2px;
}
.sort-one-add {
  display: flex;
  height: 50px;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.sort-one-add-submit {
  width: 100%;
}
.sort-one-head {
  width: 100%;
  margin-bottom: 15px;
}
.sort-one-list {
  width: 100%;
  margin-bottom: 20px;
}
.sort-one-page-line {
  width: 100%;
  display: flex;
  justify-content: flex-end;
}
</style>